// pages/app/certification/courses/courses.js
const app = getApp();
var host = app.globalData.host;
var utils = app.globalData.utils;
var self = null;

Page({
  data: {
    themeColor: '',
    themeColor2: '',
    defaultColor: '',
    borderColor: '',
    certificateId: '',
    info: {},
    list: [],
    progress: 0
  },
  onLoad: function (options) {
    self = this;
    self.getStyle();
    self.setData({
      certificateId: options.id
    })
    wx.checkSession({
      success() {
        app.login(self.route);
      },
      fail() {
        // app.loginWechat(app.login(self.route));
        app.globalData.openId = null;
        app.serverLogin({
          success() {
            console.log('自动登录成功')
          },
          fail() {
            console.log('自动登录失败')
          }
        })
      }
    })
  },
  onShow: function () {
    self.init();
  },
  getStyle() {
    self.setData({
      themeColor: app.globalData.themeColor,
      themeColor2: app.globalData.themeColor2,
      defaultColor: app.globalData.defaultColor,
      borderColor: app.globalData.borderColor
    })
  },
  init() {
    var url = host + '/lx/mini/question/info';
    var params = {
      certificateId: self.data.certificateId
    };
    var success = function (res) {
      self.setData({
        info: res.info,
        list: res.list
      });
      var passCount = 0;
      for (var i = 0; i < self.data.list.length; i ++) {
        if (self.data.list[i].userId) {
          passCount ++;
        }
      }
      var result = utils.accMul(utils.accDiv(passCount, self.data.list.length), 100);
      self.setData({
        progress: result ? result.toFixed(0) : 0
      })
    }
    var fail = function (res) {
      console.log(res);
    };
    var complete = function () { };
    utils.request(url, params, success, fail, complete);
  },
  goStudy(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/app/certification/study/study?id=' + id
    })
  },
  goExam(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/app/certification/exam/exam?id=' + id
    })
  }
})